(function($){
  var statusSearch = 0;
  var timeSearch = null;
  var delaySearch = 5000;

  var sliderCommon = function(){
    jQuery('#slider').bsSlider({
      effect: 'fade',
      pauseTime: 5000
    });
  };
  var checkSearch = function(){
    if(statusSearch == 0){
      window.clearTimeout(timeSearch);
      timeSearch = window.setTimeout(function(){
        $("#box-find").addClass("of-show");
      },0);
    }else{
      window.clearTimeout(timeSearch);
      timeSearch = window.setTimeout(function(){
        $("#box-find").removeClass("of-show");
      },delaySearch);
    }
  };

  var changeSearch = function(){
    $("#box-find").hover(function(){
      checkSearch();
      statusSearch = 1;
    },function(){
      checkSearch();
      statusSearch = 0;
    });
  };

  var animateTrigger= function(){
    jQuery(window).scroll(function() {
      jQuery(".animated-area").each(function() {
        if(jQuery(window).height() + jQuery(window).scrollTop() - jQuery(this).offset().top > 0) {
          jQuery(this).trigger("animate-it");
        }
      });
    });
    jQuery(document).ready(function(){
      jQuery(".animated-area").each(function() {
        if(jQuery(window).height() + jQuery(window).scrollTop() - jQuery(this).offset().top > 0) {
          jQuery(this).trigger("animate-it");
        }
      });
    });
  };

  var animateHandle = function(){
    animateTrigger();
    jQuery(".animated-area").on("animate-it", function() {
      var cf = jQuery(this);
      cf.find(".animated").each(function() {
        var duration = (jQuery(this).attr('data-animation-duration')==null)?"0.6s":jQuery(this).attr('data-animation-duration');
        jQuery(this).css("-webkit-animation-duration",duration);
        jQuery(this).css("-moz-animation-duration",duration);
        jQuery(this).css("-ms-animation-duration",duration);
        jQuery(this).css("animation-duration",duration);
        jQuery(this).css("-webkit-animation-delay",jQuery(this).attr("data-animation-delay"));
        jQuery(this).css("-moz-animation-delay",jQuery(this).attr("data-animation-delay"));
        jQuery(this).css("-ms-animation-delay",jQuery(this).attr("data-animation-delay"));
        jQuery(this).css("animation-delay",jQuery(this).attr("data-animation-delay"));
        jQuery(this).addClass(jQuery(this).attr("data-animation"));
      });
    });
  };

  var slideAbout = function(){
    $('#slide-about').owlCarousel({
      loop: true,
      margin: 0,
      items: 1,
      nav: false,
      dots: false,
      responsiveClass: true,
      autoplay: true,
      autoplayTimeout: 5000
    });
  };

  var slidePartner = function(){
    $('#slide-partner').owlCarousel({
      loop: true,
      margin: 10,
      items: 6,
      nav: false,
      dots: false,
      responsiveClass: true,
      autoplay: false,
      autoplayTimeout: 5000
    });
  };
  var styleFilter = function(){
    $('select.select').each(function(){
      var title = $(this).attr('title');
      if( $('option:selected', this).val() != '' ) title = $('option:selected',this).text();
      $(this)
        .css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
        .after('<span class="span"><span>' + title + '</span></span>')
        .change(function(){
          val = $('option:selected',this).text();
          $(this).next().text(val);
        })
    });
  };
  var sliderEducation = function(){
    $(".box-education .item .title").on("click",function(){
      var item = $(this).parent(".item");
      if(item.hasClass("of-show")){
        item.removeClass("of-show");
        item.find(".content").stop().hide('slow');
      }else{
        item.addClass("of-show");
        item.find(".content").stop().show('slow');
      }
    });
  };

  var popupVideo = function(){
    $('.video').attr('rel', 'media').fancybox({
      openEffect : 'none',
      closeEffect : 'none',
      prevEffect : 'none',
      nextEffect : 'none',
      title: false,
      arrows : false,
      helpers : {
        media : {},
        buttons : {}
      }
    });
  };
  var popupRegister = function(){
    $("a[rel=popup]").fancybox({
      maxWidth  : 320,
      padding : 0,
      title: false,
      cyclic : false,
      arrows : false
    });
  };
  jQuery(document).ready(function() {
    changeSearch();
    animateHandle();
    slideAbout();
    slidePartner();
    styleFilter();
    sliderEducation();
    popupVideo();
    popupRegister();
  });
  jQuery(window).load(function() {
    sliderCommon();
  });
})(jQuery);